package cn.honglian.org.controller;

import cn.honglian.basic.util.AjaxResult;
import cn.honglian.basic.util.PageList;
import cn.honglian.org.domain.Employee;
import cn.honglian.org.query.EmployeeQuery;
import cn.honglian.org.service.EmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public List<Employee> findByIds(@PathVariable Long id){
        List<Employee> byIds = employeeService.findByIds(id);
            return  byIds;
    }

    @GetMapping()
    public List<Employee> findAll(){
        return employeeService.findAll();
    }


    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery employeeQuery){

        return employeeService.queryByPage(employeeQuery);

    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids){

        try {
            employeeService.batchDelete(ids);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    @PutMapping()
    public AjaxResult addAndUpdate(@RequestBody Employee employee){

        try {
            if (null!= employee.getId()){
                employeeService.update(employee);
            } else {
                employeeService.add(employee);
            }

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }



}
